<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" size="large" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @input="handleQuery"
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="设备编码" prop="equipmentName">
        <el-input v-model="queryParams.code" placeholder="请输入设备编码" clearable @input="handleQuery"
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="安装位置" prop="installPosition">
        <el-input v-model="queryParams.installPosition" placeholder="请输入安装位置" clearable @input="handleQuery"
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="所属部门" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入所属部门" @input="handleQuery" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="设备分类" prop="equipmentClassify">
        <el-select filterable @change="handleQuery" clearable class="!w-240px" v-model="queryParams.equipmentClassify"
          placeholder="请选择设备分类">
          <el-option v-for="dict in equipmentClassify" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="equipmentStatus">
        <el-select @change="handleQuery" v-model="queryParams.equipmentStatus" placeholder="请选择设备状态" clearable
          class="!w-240px">
          <el-option clearable v-for="dict in getIntDictOptions(DICT_TYPE.EQUIPMENT_STATUS)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-select @change="handleQuery" v-model="queryParams.equipmentType" placeholder="请选择设备类型" clearable
          class="!w-240px">
          <el-option clearable v-for="dict in getIntDictOptions(DICT_TYPE.EQUIPMENT_TYPE)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['business:equipment-manage:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="warning" plain @click="handleImport" v-hasPermi="['business:equipment-manage:import']">
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['business:equipment-manage:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['business:equipment-manage:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" border v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" width="120px">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openViewDialog(row.id)">
            {{ row.equipmentName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备编码" align="center" prop="code" width="120px" />
      <el-table-column label="所属部门" align="center" prop="deptName" width="120px" />
      <el-table-column label="设备分类" align="center" prop="equipmentClassifyName" width="120px" />
      <el-table-column label="设备状态" align="center" prop="equipmentStatus" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EQUIPMENT_STATUS" :value="scope.row.equipmentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="equipmentType" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EQUIPMENT_TYPE" :value="scope.row.equipmentType" />
        </template>
      </el-table-column>
      <el-table-column label="维护人员" align="center" prop="maintenanceUserName" width="120px" />
      <el-table-column label="规格型号" align="center" prop="equipmentModel" width="120px" />
      <el-table-column label="安装位置" align="center" prop="installPosition" width="120px" />
      <el-table-column label="设备功率kw" align="center" prop="equipmentPower" width="120px" />
      <el-table-column label="设备重量" align="center" prop="equipmentWeight" width="120px" />
      <el-table-column label="设备吨位" align="center" prop="equipmentTonnage" width="120px" />
      <el-table-column label="安装日期" align="center" prop="installDate" width="120px" />
      <el-table-column label="投产日期" align="center" prop="productionDate" width="120px" />
      <el-table-column label="单位" align="center" prop="unit" width="120px" />
      <el-table-column label="设备厂家" align="center" prop="equipmentProducer" width="120px" />
      <el-table-column label="联系人" align="center" prop="contacts" width="120px" />
      <el-table-column label="联系电话" align="center" prop="contactsNumber" width="120px" />
      <el-table-column label="使用状态" align="center" prop="useType" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.USED_TYPE" :value="scope.row.useType" />
        </template>
      </el-table-column>
      <el-table-column label="最近一次保养时间" align="center" prop="maintainDatetime" :formatter="dateFormatter"
        width="180px" />
      <el-table-column label="下次保养时间" align="center" prop="nextMaintainDatetime" :formatter="dateFormatter"
        width="180px" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" min-width="150px" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:equipment-manage:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:equipment-manage:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <EquipmentManageForm ref="formRef" @success="getList" />

  <!-- 导入弹窗 -->
  <EquipmentImportForm ref="importFormRef" @success="getList" />

  <!-- 详细信息 -->
  <EquipmentInfoView ref="viewRef" />

</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import { EquipmentManageApi, EquipmentManage } from '@/api/business/equipmentmanage'
import EquipmentManageForm from './EquipmentManageForm.vue'
import { EquipmentTypeApi } from '@/api/business/equipmenttype'
import EquipmentImportForm from './EquipmentImportForm.vue'
import EquipmentInfoView from './EquipmentInfoView.vue'

/** 设备台账 列表 */
defineOptions({ name: 'EquipmentManage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<EquipmentManage[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  equipmentName: undefined,
  deptName: undefined,
  equipmentClassify: undefined,
  equipmentStatus: undefined,
  equipmentType: undefined,
  installPosition: undefined,
  code: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const equipmentClassify = ref([]) // 树形结构

//获取所有分类
const getTypeList = async () => {
  const res = await EquipmentTypeApi.getEquipmentTypeList();
  equipmentClassify.value = res
}

/** 设备台账导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}



/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EquipmentManageApi.getEquipmentManagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EquipmentManageApi.deleteEquipmentManage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除设备台账 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await EquipmentManageApi.deleteEquipmentManageList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: EquipmentManage[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EquipmentManageApi.exportEquipmentManage(queryParams)
    download.excel(data, '设备台账.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}


const tableHeaderStyle = ({ row, colum, rowIndex, columIndex }) => {
  console.log("666", colum, rowIndex, columIndex,);

  if (rowIndex == 0 && columIndex === 0) {
    console.log("sssss", rowIndex, columIndex);
    return 'background-color: #67c23a;'
  }

}

const viewRef = ref()

const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id)
}



/** 初始化 **/
onMounted(() => {
  getList()
  getTypeList()
})
</script>
